package com.qcby.framework.common.pojo;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.qcby.framework.common.exception.ServiceException;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * 统一结果封装
 *
 * @author cong.zhen
 * @date 2023/01/07
 */
public class Result<T> implements Serializable, IResult<T> {
   
    private Integer status;
    private String msg;
    private String code;
    private T data;
    private String trace;

    public Result() {
        this.status = SUCCESS_STATUS;
    }

    public Result(Integer status, String msg, String code, T data, String trace) {
        this.status = SUCCESS_STATUS;
        this.status = status;
        this.msg = msg;
        this.code = code;
        this.data = data;
        this.trace = trace;
    }

    public Result(Integer status, String msg, String code, T data) {
        this(status, msg, code, data, (String)null);
    }

    public Result(Integer status, String msg, String code) {
        this(status, msg, code, null, (String)null);
    }

    @JSONField(serialize = false)
    public boolean isSuccess() {
        return SUCCESS_STATUS.equals(this.getStatus());
    }

    public T unboxing() {
        if (this.isSuccess()) {
            return this.getData();
        } else {
            throw new ServiceException(this.getMsg(), this.getCode());
        }
    }

    public static <T> Result<T> getServiceError() {
        return getServiceError("服务器繁忙，请稍候重试！", (String)null);
    }

    public static <T> Result<T> getServiceError(String message) {
        return getServiceError(message, (String)null);
    }

    public static <T> Result<T> getServiceError(String message, String trace) {
        Result<T> result = new Result(SERVICE_ERROR_STATUS, message, (String)null, (Object)null, trace);
        return result;
    }

    public static <T> Result<T> getSuccessResult(T re) {
        Result<T> result = new Result(SUCCESS_STATUS, (String)null, (String)null, re);
        return result;
    }

    public static <T> Result<T> getBusinessException(String msg, String code) {
        Result<T> result = new Result(BUSINESS_ERROR_STATUS, msg, code, (Object)null);
        return result;
    }

    public static <T> Result<T> getBusinessException(String code) {
        return getBusinessException((String)null, code);
    }

    public static <T> Result<T> getParamException(String code) {
        return getParamException(code, (String)null);
    }

    public static <T> Result<T> getParamException(String code, String msg) {
        return new Result(PARAM_ERROR_STATUS, msg, code);
    }

    public static List copyPropertiesForCollection(Collection source, Class targetClass) {
        List result = new ArrayList();
        if (source != null) {
            Iterator var3 = source.iterator();

            while(var3.hasNext()) {
                Object item = var3.next();

                try {
                    Object targetObject = targetClass.newInstance();

                    BeanUtil.copyProperties(item, targetObject);
                    result.add(targetObject);
                } catch (Exception var6) {
                    throw new RuntimeException(var6);
                }
            }
        }

        return result;
    }


    @Override
    public int getStatus() {
        return this.status;
    }


    @Override
    public String getMsg() {
        return this.msg;
    }


    @Override
    public T getData() {
        return this.data;
    }

    @Override
    public String getCode() {
        return this.code;
    }

    @Override
    public String getTrace() {
        return this.trace;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("Result{");
        sb.append("status=").append(this.status);
        sb.append(", msg='").append(this.msg).append('\'');
        sb.append(", code='").append(this.code).append('\'');
        sb.append(", data=").append(this.data);
        sb.append('}');
        return sb.toString();
    }
}